<!doctype html>
<html>
<head>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8">
<title>Test for Range.expand()</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>
function expandRangeString(startId, startOffset, endId, endOffset, expandUnit, firstChild) {
  const range = document.createRange();
  let startNode = document.getElementById(startId);
  if (firstChild == true)
    startNode = startNode.firstChild;
  let endNode = document.getElementById(endId);
  if (firstChild == true)
    endNode = endNode.firstChild;
  range.setStart(startNode, startOffset);
  range.setEnd(endNode, endOffset);
  range.expand(expandUnit);
  return range.toString();
}

function runTests() {
  // test 1 - Expand to document.
  test(() => {
    const actual = expandRangeString('myspan', 0, 'myspan', 0, 'document', false);
    const expected = 'This is the begin of a block. A block is a collection of sentences. Each sentence begins with capital\x0Aletter and ends with a punctuation.\x0A\x0ANow, a sentence\x0Ais divided into\x0Amultiple lines.\x0A\x0ARoll the mouse around the page. The coordinates\x0Aof the mouse pointer are currently atop an elementwhose ID \x0Ais:"".\x0A\x0A\x0A\x0A    Cell A1\x0A    Cell B1\x0A\x0A\x0A    Cell A2\x0A    Cell B2\x0A\x0A\x0ASection 1\x0Atext.\x0ASection 2\x0Atext.\x0A\x0AHere is a positioned element.\x0A在甲型H1N1流感可能出现大流行的警告声中，昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会，今年足足提前了一个月；会上研讨的主题，已从以前的季节性流感，变成以甲型H1N1流感为重点。\x0Aבלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"'
    assert_equals(actual, expected);
  }, 'document1');

  // test 2 - Expand to block.
  test(() => {
    const actual = expandRangeString('td_A1', 0, 'td_A1', 0, 'block', false);
    assert_equals(actual, 'Cell A1');
  }, 'block2');

  // test 3 - Expand to block, different range start and end offset.
  test(() => {
    const actual = expandRangeString('td_A1', 1, 'td_A1', 2, 'block', true);
    assert_equals(actual, 'Cell A1');
  }, 'block3');

  // test 4 - Expand to block, different range start and end node.
  test(() => {
    const actual = expandRangeString('td_A1', 0, 'td_B1', 0, 'block', false);
    assert_equals(actual, 'Cell A1\x0A    Cell B1');
  }, 'block4');

  // test 5 - Expand to sentence.
  test(() => {
    const actual = expandRangeString('instructions', 0, 'instructions', 0, 'sentence', false);
    assert_equals(actual, 'Roll the mouse around the page. ');
  }, 'sentence5');

  // test 6 - Expand to sentence, different range start and end offset.
  test(() => {
    const actual = expandRangeString('instructions', 1, 'instructions', 2, 'sentence', true);
    assert_equals(actual, 'Roll the mouse around the page. ');
  }, 'sentence6');

  // test 7 - Expand to sentence, different range start and end node.
  test(() => {
    const actual = expandRangeString('instructions', 0, 'mybr', 0, 'sentence', false);
    assert_equals(actual, 'Roll the mouse around the page. The coordinates\x0Aof the mouse pointer are currently atop an element');
  }, 'sentence7');

  // test 8
  test(() => {
    const actual = expandRangeString('multilineSentence', 0, 'multilineSentence', 0, 'sentence', false);
    assert_equals(actual, 'Now, a sentence\n');
  }, 'sentence8');

  test(() => {
    const actual = expandRangeString('multilineSentence', 1, 'multilineSentence', 5, 'sentence', true);
    assert_equals(actual, 'Now, a sentence\n');
  }, 'sentence9');

  test(() => {
    const actual = expandRangeString('multilineSentence', 20, 'multilineSentence', 20, 'sentence', true);
    assert_equals(actual, 'is divided into\n');
  }, 'sentence10');

  test(() => {
    const actual = expandRangeString('multilineSentence', 40, 'multilineSentence', 40, 'sentence', true);
    assert_equals(actual, 'multiple lines.');
  }, 'sentence11');

  // Sentence end expansion should not cross block boundaries.
  test(() => {
    const id = 'begin';
    const length = document.getElementById(id).firstChild.length;
    const actual = expandRangeString(id, length, id, length, 'sentence', true);
    assert_equals(actual, 'Each sentence begins with capital\nletter and ends with a punctuation.');
  }, 'sentence12');

  // Expand word.
  // Same range start and end, both at the begin of word.
  test(() => {
    const actual = expandRangeString('mydiv', 0, 'mydiv', 0, 'word', false);
    assert_equals(actual, 'Here');
  }, 'word1');

  // Same range start and end, both at the begin of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 1, 'mydiv', 1, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word2');

  // Same range start and end, both at the middle of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 3, 'mydiv', 3, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word3');

  // Same range start and end, both at the end of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 4, 'mydiv', 4, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word4');

  // Different range start and end offset, both at the middle of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 2, 'mydiv', 3, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word5');

  // Different range start and end offset, start at the begin of a word,
  // and end at the end of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 5, 'mydiv', 4, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word6');

  // Different range start and end offset, start at the begin of a word,
  // and end at the middle of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 1, 'mydiv', 3, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word7');

  // Different range start and end offset, start at the middle of a word,
  // and end at the end of a word.
  test(() => {
    const actual = expandRangeString('mydiv', 2, 'mydiv', 4, 'word', true);
    assert_equals(actual, 'Here');
  }, 'word8');

  // Across more than 1 word.
  // Start at the begin of 1st word, end at the end of last word.
  test(() => {
    const actual = expandRangeString('mydiv', 1, 'mydiv', 7, 'word', true);
    assert_equals(actual, 'Here is');
  }, 'word9');

  // Start at the middle of 1st word, end at the middle of last word.
  test(() => {
    const actual = expandRangeString('mydiv', 2, 'mydiv', 6, 'word', true);
    assert_equals(actual, 'Here is');
  }, 'word10');

  // Start at the begin of 1st word, end at the middle of last word.
  test(() => {
    const actual = expandRangeString('mydiv', 1, 'mydiv', 6, 'word', true);
    assert_equals(actual, 'Here is');
  }, 'word11');

  // Start at the middle of 1st word, end at the end of last word.
  test(() => {
    const actual = expandRangeString('mydiv', 2, 'mydiv', 7, 'word', true);
    assert_equals(actual, 'Here is');
  }, 'word12');

  // Word and space.
  test(() => {
    const actual = expandRangeString('mydiv', 4, 'mydiv', 5, 'word', true);
    assert_equals(actual, 'Here ');
  }, 'word13');

  // Word across different nodes.
  test(() => {
    const actual = expandRangeString('mydiv', 1, 'he-div', 0, 'word', true);
    assert_equals(actual, 'Here is a positioned element.\x0A在甲型H1N1流感可能出现大流行的警告声中，昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会，今年足足提前了一个月；会上研讨的主题，已从以前的季节性流感，变成以甲型H1N1流感为重点。\x0Aבלשכת');
  }, 'word14');

  // Test for Chinese. No Chinese segmentation in platform/mac.
  test(() => {
    const actual = expandRangeString('zh-CN-div', 0, 'zh-CN-div', 0, 'word', true);
    assert_equals(actual, '在');
  }, 'Chinese word0');

  test(() => {
    const actual = expandRangeString('zh-CN-div', 1, 'zh-CN-div', 1, 'word', true);
    assert_equals(actual, '甲');
  }, 'Chinese word1');

  test(() => {
    const actual = expandRangeString('zh-CN-div', 2, 'zh-CN-div', 2, 'word', true);
    assert_equals(actual, '型');
  }, 'Chinese word2');

  test(() => {
    const actual = expandRangeString('zh-CN-div', 3, 'zh-CN-div', 3, 'sentence', true);
    assert_equals(actual, '在甲型H1N1流感可能出现大流行的警告声中，昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。');
  }, 'Chinese sentence');

  test(() => {
    const actual = expandRangeString('zh-CN-div', 3, 'zh-CN-div', 3, 'block', true);
    assert_equals(actual, '在甲型H1N1流感可能出现大流行的警告声中，昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会，今年足足提前了一个月；会上研讨的主题，已从以前的季节性流感，变成以甲型H1N1流感为重点。');
  }, 'Chinese block');

  // Test for Hebrew.
  test(() => {
    const actual = expandRangeString('he-div', 1, 'he-div', 1, 'word', true);
    assert_equals(actual,  'בלשכת');
  }, 'Hebrew word');

  test(() => {
    const actual = expandRangeString('he-div', 1, 'he-div', 1, 'sentence', true);
    assert_equals(actual, 'בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"' );
  }, 'Hebrew sentence');

  test(() => {
    const actual = expandRangeString('he-div', 0, 'he-div', 0, 'sentence', false);
    assert_equals(actual, 'בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"'    );
  }, 'Hebrew block');
}
</script>
<body>
<p id="begin">This is the begin of a block. A block is a collection of sentences. Each sentence begins with capital
letter and ends with a punctuation.
</p>
<pre id="multilineSentence">Now, a sentence
is divided into
multiple lines.
</pre>
<p id="instructions">Roll the mouse around the page. The coordinates
of the mouse pointer are currently atop an element<br id="mybr">whose ID 
is:"<span id="myspan" style="font-weight:bold"></span>".</p>

<table border=1 id="myTable">
<tr id="tr1">
    <td id="td_A1">Cell A1</td>
    <td id="td_B1">Cell B1</td>
</tr>
<tr id="tr2">
    <td id="td_A2">Cell A2</td>
    <td id="td_B2">Cell B2</td>
</tr>
</table>
<h2 id="sec1">Section 1</h2>
<p id="p1">text.</p>
<h2 id="sec2">Section 2</h2>
<p id="p2">text.</p>
<div id="mydiv" style="position:absolute; top:340; left:300; background-color:yellow">
Here is a positioned element.</div>
<div id="zh-CN-div">在甲型H1N1流感可能出现大流行的警告声中，昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会，今年足足提前了一个月；会上研讨的主题，已从以前的季节性流感，变成以甲型H1N1流感为重点。</div>
<div id="he-div">בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"</div>

<script>
runTests();
</script>
</body>
</html>
